package com.zimmur.app.mocolink.manage.web.vogData;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.apiData.GoodsData;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.service.RedisCacheUtil;

/**
 * vog获取更新商品接口
 * @Description
 * @author zcx
 * @time 2017年11月7日 下午2:10:00
 */
public class TakeGoods {
	private static final Logger log = LoggerFactory.getLogger(TakeGoods.class);
	private static final String apiUrl="/api/Product/getProduct";
	public static List<GoodsData> getGoods(Integer timestamp){
		Integer pageIndex=1;
		Integer pageSize=100;
		Integer enterId=1;
		String channel="W";
		List<GoodsData> datas = new ArrayList<>();
		while(true){
			List<NameValuePair> params = new ArrayList<>();
			params.add(new BasicNameValuePair("pageIndex",pageIndex+""));
			params.add(new BasicNameValuePair("pageSize",pageSize+""));
			params.add(new BasicNameValuePair("EnterId",enterId+""));
			params.add(new BasicNameValuePair("Channel",channel+""));
			params.add(new BasicNameValuePair("Timestamp",timestamp+""));
			String result = VogDataRequestUtil.getRequest(params, apiUrl);
//			System.out.println(result);
			if(StringUtils.isBlank(result)){
				log.warn("TakeGoods false,result null");
				break;
			}
			if(log.isDebugEnabled()){
				log.debug("take goods:["+result+"]");
			}
			JSONObject json =JSONObject.parseObject(result);
			if(!json.getBooleanValue("Success")){
				log.warn("TakeGoods false,info:{}",result);
				break;
			}
			String data = json.getJSONObject("Data").toJSONString();
			PageData<GoodsData> page = JSONObject.parseObject(data,PageData.class);
			if(page==null || page.getPageCount()==null || page.getResult()==null || page.getResult().size()==0){
				break;
			}else{
				//String resultData=json.getJSONObject("Data").getJSONArray("Result").toJSONString();
				String resultData=json.getJSONObject("Data").getJSONArray("Result").toJSONString();
				List<GoodsData> list = JSONObject.parseArray(resultData, GoodsData.class);
				datas.addAll(list);
				pageIndex++;
				if(page.getPageCount()<pageIndex){
					break;
				}
			}
		}
		
		return datas;
	}
}
